<template>
  <div>
    <div>
      <el-form ref="form" :model="sysUser" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="所在部门" prop="deptId">
              <treeselect
                v-model="sysUser.deptId"
                :options="deptList"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择所在部门"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="账号" prop="userName">
              <el-input v-model="sysUser.userName" placeholder="请输入账号" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="sysUser.nickName" placeholder="请输入昵称" clearable/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="sysUser.email" placeholder="请输入邮箱" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="sysUser.phone" placeholder="请输入手机号" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="sysUser.sex">
                <el-radio v-for="status in sexOptions" :key="status.dictValue" :label="status.dictValue">
                  {{ status.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="sysUser.status">
                <el-radio v-for="status in statusOptions" :key="status.dictValue" :label="status.dictValue">
                  {{ status.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="直属领导" prop="leaderName">
              <el-input v-model="sysUser.leaderName" placeholder="请选择直属领导" disabled style="width: 60%" clearable/>
              <el-button type="primary" style="width: 35%;margin-left: 5px" @click="selectLeader">选择</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="text-align: center">
          <el-button type="primary" @click="update">修改</el-button>
          <el-button type="warning" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!--选择用户的组件-->
      <select-system-user ref="selectUser" v-if="showSelectUserDialog" @select="confirmSelectUser"></select-system-user>
    </div>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import selectSystemUser from '@/components/selectSystemUser/index'
import sysDeptApi from "@/api/system/sysDept";
import sysUserApi from "@/api/system/sysUser";
import {emptyStringToNull} from "@/utils/form"

export default {
  name: "sys-user-add",
  components: {
    Treeselect,
    selectSystemUser
  },
  props: {
    activeId: {
      type: Number,
      default: undefined
    }
  },
  watch: {
    activeId: {
      immediate: true,
      handler: function (newVal, oldVal) {
        this.getById(newVal);
      }
    }
  },
  data() {
    return {
      sysUser: {
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        leaderId: undefined,
        leaderName: undefined,
        email: undefined,
        phone: undefined,
        sex: 0,
        status: 0,
      },
      //部门的下接树的数据
      deptList: [],
      //状态字典数据
      statusOptions: [],
      //性别的字典数据
      sexOptions: [],
      //是否打开选择用户的弹出层组件
      showSelectUserDialog: false
    }
  },
  created() {
    sysDeptApi.listDept().then(res => {
      this.deptList = res.data
    })
    //加载状态的字典数据
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
    this.getDataByType("sys_user_sex").then(res => {
      this.sexOptions = res.data;
    })
  },
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    //根据用户ID查询用户
    getById(userId) {
      sysUserApi.get(userId).then(res => {
        this.sysUser = res.data;
      })
    },
    //修改
    update() {
      let updateData = emptyStringToNull(this.sysUser)
      sysUserApi.update(updateData).then(res => {
        this.$message.success(res.msg)
        this.$emit("after")
        this.$emit("close")
      })
    },
    // 重置修改表单
    resetForm() {
      this.getById(this.activeId)
    },
    //选择直属领导
    selectLeader() {
      this.showSelectUserDialog = true;
      this.$nextTick(_ => {
        this.$refs.selectUser.initSelectUser();
      })
    },
    //选择用户的弹出层触发之后确定之后回调的方法
    confirmSelectUser(user) {
      this.sysUser.leaderName = user.nickName;
      this.sysUser.leaderId = user.userId
    }
  },
}
</script>

<style scoped>

</style>
